package cn.lili.modules.grab.dingdong.service.impl;

import cn.lili.common.exception.ServiceException;
import cn.lili.modules.grab.dingdong.DingdongBody;
import cn.lili.modules.grab.dingdong.reps.adjunct.AreaRespose;
import cn.lili.modules.grab.dingdong.reps.adjunct.RegionAllRespose;
import cn.lili.modules.grab.dingdong.res.adjunct.RegionAllRequest;
import cn.lili.modules.grab.dingdong.res.adjunct.AreaRequest;
import cn.lili.modules.grab.dingdong.facade.DingdongFacade;
import cn.lili.modules.grab.dingdong.service.DingdongAbsService;
import cn.lili.modules.grab.dingdong.config.DingdongConfig;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.service.DingdongAdjunctService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DingdongAdjunctServiceImpl extends DingdongAbsService implements DingdongAdjunctService {

    @Resource
    private DingdongFacade dingdongFacade;
    @Resource
    private DingdongConfig dingdongConfig;

    @Override
    public List<AreaRespose> area(AreaRequest areaRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(areaRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getAreaUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<AreaRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), AreaRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<RegionAllRespose> regionAll(RegionAllRequest regionAllRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(regionAllRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getRegionAllUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<RegionAllRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), RegionAllRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }
}
